package Demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-13
 * Time: 20:10
 */
public class MyQueue {

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first = null;
    public ListNode last = null;

    public int usedSize = 0;

    public void offer(int val) {
        ListNode newNode = new ListNode(val);
        if(first == null){
            first = last = newNode;
            return;
        }else{
            last.next = newNode;
            newNode.prev = last;
            last = newNode;
        }
        usedSize++;
    }

    public int poll() {
        //空链表
        if(first == null){
            return -1;
        }
        int val = first.val;
        first.next.prev = null;
        first = first.next;
        usedSize--;
        return val;
    }

    public int peek() {
        if(first == null){
            return -1;
        }
        return first.val;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }
}
